﻿// 
// DotNetNuke?- http://www.dotnetnuke.com 
// Copyright (c) 2002-2009 
// by DotNetNuke Corporation 
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions: 
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software. 
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE. 
// 

using System;
using System.Configuration;
using System.Data;
using System.Xml;
using System.Web;
using System.Collections.Generic;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.Search;
using DotNetNuke.Entities.Modules;

namespace gygac.Modules.DeviceDeploy
{

    /// ----------------------------------------------------------------------------- 
    /// <summary> 
    /// The Controller class for DeviceDeployController 
    /// </summary> 
    /// <remarks> 
    /// </remarks> 
    /// <history> 
    /// </history> 
    /// ----------------------------------------------------------------------------- 
    public class DeviceDeployController : ISearchable, IPortable
    {
		#region Const Member		
		protected const string FIELDLIST	=	"*";		//"articleid";
		protected const string ORDERFIELD	="CreateDate";
		protected const string KEYFIELD	=" DDId ";
		protected const string STRWHERE	= "";
		protected const int	 ORDERTYPE	=1;					//1,降序,0,升序 
		#endregion
		
        #region "Public Methods"
		/// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// gets an object from the database 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
		/// <param name="DDId">The Id of the item</param>        
        /// <history> 
        /// </history> 
        /// -----------------------------------------------------------------------------        
		public DeviceDeployInfo GetDeviceDeploy(int DDId)
        {
            return (DeviceDeployInfo)CBO.FillObject(DataProvider.Instance().GetDeviceDeploy(DDId), typeof(DeviceDeployInfo));
        }
		
		/// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// 获取DeviceDeploy对象List，无参数
        /// </summary> 
        /// <remarks> 
        /// </remarks> 		
        /// <history> 
        /// </history> 
		public List<DeviceDeployInfo> GetDeviceDeploys()
        {
            return CBO.FillCollection<DeviceDeployInfo>(DataProvider.Instance().GetDeviceDeploys());
        }		
		
		/// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// 获取指定ModuleId 的DeviceDeploy对象列表
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="ModuleId ">模块编号</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 			
        public List<DeviceDeployInfo> GetDeviceDeploys(int ModuleId )
        {
            return CBO.FillCollection<DeviceDeployInfo>(DataProvider.Instance().GetDeviceDeployByModuleId (ModuleId ));
        }			
		
		/// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// 取指定页序、每页记录数的DeviceDeploy对象列表
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="PageIndex">页序</param> 
		/// <param name="PageSize">每页记录数</param> 
		/// <param name="PageCount">返回的页总数</param> 
        /// <history> 
        /// </history> 
		public List<DeviceDeployInfo> GetDeviceDeploys(int PageIndex, int PageSize, ref int PageCount)
        {
			return GetDeviceDeploys(PageIndex, PageSize,ref PageCount, null, null);

        }		
		
		/// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// 取指定页序、每页记录数、指定模块编号的DeviceDeploy对象列表
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="PageIndex">页序</param> 
		/// <param name="PageSize">每页记录数</param> 
		/// <param name="PageCount">返回的页总数</param> 
		/// <param name="ModuleId">模块编号</param>
        /// <history> 
        /// </history> 
		public List<DeviceDeployInfo> GetDeviceDeploys(int PageIndex, int PageSize, ref int PageCount, int ModuleId)
        {
			string strWhere = " ModuleId = " + ModuleId.ToString();
            IDataReader dr = DataProvider.Instance().GetDeviceDeploys(PageIndex, PageSize, FIELDLIST, ORDERFIELD, KEYFIELD, strWhere, ORDERTYPE);
            dr.Read();
            int Rcount = Convert.ToInt32(dr[0]);			
            if ((Rcount % PageSize) > 0)
            {
                PageCount = (Rcount / PageSize) + 1;
            }
            else
            {
                PageCount = Rcount / PageSize;
            }	
            //List<DeviceDeployInfo> = new List<DeviceDeployInfo>();
            return CBO.FillCollection<DeviceDeployInfo>(dr);       
        }		
		
		/// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// 取指定页序、每页记录数、指定模块编号的DeviceDeploy对象列表
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="PageIndex">页序</param> 
		/// <param name="PageSize">每页记录数</param> 
		/// <param name="PageCount">返回的页总数</param> 
		/// <param name="SearchCondition1">搜索的初始条件</param> 
		/// <param name="SearchCondition2">搜索的结束条件</param> 
        /// <history> 
        /// </history> 
		public List<DeviceDeployInfo> GetDeviceDeploys(int PageIndex, int PageSize, ref int PageCount, DeviceDeployInfo SearchCondition1, DeviceDeployInfo SearchCondition2)
        {
			string strWhere = string.Empty;
			if(SearchCondition2 == null)
			{
				if(SearchCondition1==null)
				{
					strWhere = STRWHERE;
				}
				else
				{
				}
			}
			else
			{
				
			}
			IDataReader dr = DataProvider.Instance().GetDeviceDeploys(PageIndex, PageSize, FIELDLIST, ORDERFIELD, KEYFIELD, STRWHERE,  ORDERTYPE);		
            dr.Read();
            int Rcount = Convert.ToInt32(dr[0]);			
            if ((Rcount % PageSize) > 0)
            {
                PageCount = (Rcount / PageSize) + 1;
            }
            else
            {
                PageCount = Rcount / PageSize;
            }	
            //List<DeviceDeployInfo> = new List<DeviceDeployInfo>();
            return CBO.FillCollection<DeviceDeployInfo>(dr);            
        }	
		
        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// adds an object to the database 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="objDeviceDeploy">The DeviceDeployInfo object</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 		
        public void AddDeviceDeploy(DeviceDeployInfo objDeviceDeploy) 
        {

            //if (objDeviceDeploy.Content.Trim() != "")
            //{
                DataProvider.Instance().AddDeviceDeploy(objDeviceDeploy.ModuleId, objDeviceDeploy.DeviceName, objDeviceDeploy.Borrower, objDeviceDeploy.Lender, objDeviceDeploy.LendDate, objDeviceDeploy.Estimated, objDeviceDeploy.IsReturned, objDeviceDeploy.ReturnDate, objDeviceDeploy.EMail, objDeviceDeploy.CreatedByUser, objDeviceDeploy.Remark);
            //}

        }
        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// saves an object to the database 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="objDeviceDeploy">The DeviceDeployInfo object</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public void UpdateDeviceDeploy(DeviceDeployInfo objDeviceDeploy)
        {

            //if (objDeviceDeploy.Content.Trim() != "")
            //{
                DataProvider.Instance().UpdateDeviceDeploy(objDeviceDeploy.DDId, objDeviceDeploy.ModuleId, objDeviceDeploy.DeviceName, objDeviceDeploy.Borrower, objDeviceDeploy.Lender, objDeviceDeploy.LendDate, objDeviceDeploy.Estimated, objDeviceDeploy.IsReturned, objDeviceDeploy.ReturnDate, objDeviceDeploy.EMail, objDeviceDeploy.CreatedByUser, objDeviceDeploy.Remark);
            //}

        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// deletes an object from the database 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
		/// <param name="DDId">The Id of the item</param> 
        /// <param name="ModuleId">The Id of the module</param>         
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public void DeleteDeviceDeploy(int DDId)
        {
            DataProvider.Instance().DeleteDeviceDeploy(DDId);
        }

        #endregion

        #region "Optional Interfaces"

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// GetSearchItems implements the ISearchable Interface 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="ModInfo">The ModuleInfo for the module to be Indexed</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public DotNetNuke.Services.Search.SearchItemInfoCollection GetSearchItems(ModuleInfo ModInfo)
        {

            SearchItemInfoCollection SearchItemCollection = new SearchItemInfoCollection();

            List<DeviceDeployInfo> colDeviceDeploys = GetDeviceDeploys(ModInfo.ModuleID);
            foreach (DeviceDeployInfo objDeviceDeploy in colDeviceDeploys)
            {
                //SearchItemInfo SearchItem = new SearchItemInfo(ModInfo.ModuleTitle, objDeviceDeploy.Content, objDeviceDeploy.CreatedByUser, objDeviceDeploy.CreatedDate, objDeviceDeploy.DDId.ToString(), ModInfo.ModuleID, objDeviceDeploy.Content, "DDId=" + objDeviceDeploy.DDId.ToString());
                //SearchItemCollection.Add(SearchItem);
            }

            return SearchItemCollection;

        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// ExportModule implements the IPortable ExportModule Interface 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="ModuleID">The Id of the module to be exported</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public string ExportModule(int ModuleID)
        {

            string strXML = "";

            List<DeviceDeployInfo> colDeviceDeploys = GetDeviceDeploys(ModuleID);
            if (colDeviceDeploys.Count != 0)
            {
                strXML += "<DeviceDeploys>";
                foreach (DeviceDeployInfo objDeviceDeploy in colDeviceDeploys)
                {
                    //strXML += "<DeviceDeploy>";
                    //strXML += "<content>" + XmlUtils.XMLEncode(objDeviceDeploy.Content) + "</content>";
                    //strXML += "</DeviceDeploy>";
                }
                strXML += "</DeviceDeploys>";
            }

            return strXML;

        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// ImportModule implements the IPortable ImportModule Interface 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="ModuleID">The Id of the module to be imported</param> 
        /// <param name="Content">The content to be imported</param> 
        /// <param name="Version">The version of the module to be imported</param> 
        /// <param name="UserId">The Id of the user performing the import</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public void ImportModule(int ModuleID, string Content, string Version, int UserId)
        {

            XmlNode xmlDeviceDeploys = Globals.GetContent(Content, "DeviceDeploys");
            foreach (XmlNode xmlDeviceDeploy in xmlDeviceDeploys.SelectNodes("DeviceDeploy"))
            {
                //DeviceDeployInfo objDeviceDeploy = new DeviceDeployInfo();
                //objDeviceDeploy.ModuleId = ModuleID;
                //objDeviceDeploy.Content = xmlDeviceDeploy.SelectSingleNode("content").InnerText;
                //objDeviceDeploy.CreatedByUser = UserId;
                //AddDeviceDeploy(objDeviceDeploy);
            }

        }

        #endregion

    }
}